package com.ymatou.c2cliveindexer.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.c2cliveindexer.parameter.LiveSearchBean;
import com.ymatou.c2cliveindexer.parameter.LiveSearchReturnBean;
import com.ymatou.c2cliveindexer.parameter.LiveindexerBean;
import com.ymatou.c2cliveindexer.service.LiveSearchCall;
import com.ymatou.c2cliveindexer.service.LiveindexerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtC2CLiveActivity;
import com.ymttest.database.sqlwapper.YmtC2CLiveActivityWapper;

/**
 * 
 * 
 * 
 * 该文件已废弃
 * 
 * 
 * 
 * 
 * */
/**
 * c2clive搜索首页列表接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_LiveSearch {
	private static LiveindexerBean liveindexerBean;
	private static LiveindexerCall liveindexerCall;
	private static LiveSearchBean liveSearchBean;
	private static LiveSearchCall liveSearchCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("c2clive搜索首页列表接口");
		try {
			liveindexerBean = new LiveindexerBean();
			liveindexerCall = new LiveindexerCall();
			liveindexerBean.setAction("reload");
			liveindexerCall.setData(liveindexerBean);
			liveindexerCall.callService();
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		liveSearchBean = new LiveSearchBean();
		liveSearchCall = new LiveSearchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(liveSearchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LiveSearch_001() {
		Logger.start(true,"搜索首页列表返回结果与数据库值比较");
		try {
			Thread.sleep(3000);
			liveSearchCall
					.setData("query=*&fl=id,isReplay,activityName,content,location,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit=0,100");
			// liveSearchCall.setData("");

			liveSearchCall.callService();
			// liveSearchCall.callService();
			Thread.sleep(2000);
			
			Logger.verifyEquals("OK", liveSearchCall.getStatus(), "验证返回状态");
			// 验证数据库
			List<YmtC2CLiveActivity> list = new YmtC2CLiveActivityWapper().getsLiveActivity();
			List<LiveSearchReturnBean> returnList = liveSearchCall.getHitsList();
		
			// 升序排序
			Collections.sort(returnList);
			Logger.verifyEquals(liveSearchCall.getTotal(), returnList.size(), "验证总条数");
			Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
			if ((returnList.size() > 0) && (list.size() == returnList.size())) {
				for (int i = 0; i < list.size(); i++) {
					Logger.verifyEquals(list.get(i).getIactivityid(), returnList.get(i).getId(), "验证数据库与返回的ctivityid");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LiveSearch_002() {
		Logger.start(true,"搜索首页列表成功");
		try {
			liveSearchCall
					.setData("fl=id,isReplay,activityName,content,location,brands,sellerName,sellerId,countryId,beginTime,endTime,addTime&limit=0,500&sort=desc(yscore)");
			liveSearchCall.callService();
			// liveSearchCall.callService();
			Thread.sleep(2000);
			Logger.debug("发送报文:" + liveSearchCall.getEntityString());
			Logger.debug("返回报文:" + liveSearchCall.getReturnData());
			Logger.verifyEquals("OK", liveSearchCall.getStatus(), "验证返回状态");
			// 验证数据库
			List<YmtC2CLiveActivity> list = new YmtC2CLiveActivityWapper().getsLiveActivity();
			ArrayList<LiveSearchReturnBean> returnList = liveSearchCall.getHitsList();
			// 升序排序
			Collections.sort(returnList);
			Logger.verifyEquals(liveSearchCall.getTotal(), returnList.size(), "验证总条数");
			Logger.verifyEquals(list.size(), returnList.size(), "验证数据库与返回的数量");
			if ((returnList.size() > 0) && (list.size() == returnList.size())) {
				for (int i = 0; i < list.size(); i++) {
					Logger.verifyEquals(list.get(i).getIactivityid(), returnList.get(i).getId(), "验证数据库与返回的ctivityid");
				}

			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}